<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\Content;
use app\common\model\WechatAutoreplay;
use app\common\validate\Autoreplay;
use think\facade\Request;
use think\facade\View;
use EasyWeChat\Factory;
use think\exception\ValidateException;
/**
 * Class Awechat
 * @package app\controller
 * 后台 微信相关配置文件 包含了验证
 */
class AdminWechat extends AdminInit
{
    public function __construct()
    {
        parent::__construct();

    }

    public function server()
    {
        $app = Factory::officialAccount(config("wechat"));

        $response = $app->server->serve();

        // 将响应输出
        $response->send();exit; // Laravel 里请使用：return $response;
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        
        return  view();
    }

    public function create()
    {
        if(Request::isAjax()){
            $d = input("param.");

            try {
                $result = validate(Autoreplay::class)->check($d);
                if (true !== $result) {
                    // 验证失败 输出错误信息
                    return json(["code"=>4000,"msg"=>$result]);
                }

            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return json(["code"=>4000,"msg"=>$e->getError()]);
            }

            if(isset($d["id"])){
                $res = WechatAutoreplay::update($d);
            }else{
                $res = WechatAutoreplay::create($d);
            }

            if($res){
                return  json(["code"=>200,"msg"=>"成功"]);
            }else{
                return  json(["code"=>4000,"msg"=>"请稍后再试"]);
            }
        }
        return  view();
    }

    public function delete($id)
    {
        $res = WechatAutoreplay::destroy($id);
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }
    public function auto_replay()
    {
        $list = WechatAutoreplay::where(1)->paginate();
        View::assign("list",$list);
        return  view();
    }

    public function edit($id)
    {
        $list = WechatAutoreplay::where("id",$id)->find();
        View::assign("res",$list);
        return  view("create");
    }



    public function menu()
    {

        $app = Factory::officialAccount(config("wechat"));

        $app->server->push(function ($message) {
            switch ($message['MsgType']) {
                case 'event':
                    return '收到事件消息';
                    break;
                case 'text':
                    return '收到文字消息';
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }

            // ...
        });


        $buttons = [
            [
                "type" => "click",
                "name" => "今日歌曲",
                "key"  => "V1001_TODAY_MUSIC"
            ],
            [
                "name"       => "菜单",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "搜索",
                        "url"  => "http://www.soso.com/"
                    ],
                    [
                        "type" => "view",
                        "name" => "视频",
                        "url"  => "http://v.qq.com/"
                    ],
                    [
                        "type" => "click",
                        "name" => "赞一下我们",
                        "key" => "V1001_GOOD"
                    ],
                ],
            ],
        ];
        $res = $app->menu->create($buttons);
        return  view();
    }

    //保存配置文件
    public function save()
    {
        $arr = input("post.");
        //将数组写入配置文件
        $a = var_export($arr,true);
        $res = file_put_contents("./config/wechat.php","<?php \n return ".$a.';');

        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }


}
